package com.supermap.dataservice.project.mapper;

import com.supermap.dubbo.api.product.entity.ExpertConsultation;
import com.supermap.dubbo.api.product.entity.ExpertConsultationFile;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author:zhoujian
 * @Desciption:
 * @date:2019-07-24 10:37
 * @version:v1.0.0
 */
@Mapper
public interface ExpertConsultationMapper {

    @Insert("insert into t_expert_consultation(title,initiate_user_id,initiate_user_name,join_user_id,join_user_name) " +
            "values(#{title},#{initiateUserId} , #{initiateUserName},#{initiateUserId},#{initiateUserName})")
    @Options(useGeneratedKeys=true, keyProperty="id", keyColumn="id")
    int insert(ExpertConsultation expertConsultation);

    @Update("update t_expert_consultation t " +
            "set " +
            "t.join_user_id = concat(t.join_user_id , \",\" , #{joinUserIds})," +
            "t.join_user_name = concat(t.join_user_name ,\",\",#{joinUserNames}) " +
            " where t.id = #{id} ")
    int addJoinUser(@Param("id") int id, @Param("joinUserIds") String joinUserIds, @Param("joinUserNames") String joinUserNames);

    @Update("update t_expert_consultation t " +
            "set " +
            "t.join_user_id = replace(t.join_user_id , concat(\",\",#{joinUserId}),\"\") , " +
            "t.join_user_name = replace(t.join_user_name , concat(\",\",#{joinUserName}) , \"\") " +
            "where t.id =#{id} " +
            "and t.initiate_user_id = #{initiateUserId}")
    int  delJoinUser(@Param("id") int id, @Param("initiateUserId") String initiateUserId, @Param("joinUserId") String joinUserId, @Param("joinUserName") String joinUserName);

    @Update("update t_expert_consultation t " +
            "set" +
            " t.`status` = 0 " +
            "where " +
            "t.id = #{id} " +
            "and t.initiate_user_id = #{initiateUserId}")
    int delConsultation(@Param("id") int id, @Param("initiateUserId") String initiateUserId);

    @Update("update t_expert_consultation t set t.chat_file = #{chatFile} where t.id = #{id}")
    int updateChatFileById(@Param("id") int id , @Param("chatFile") String chatFile);

    @Select("select \n" +
            "t.id , t.title, \n" +
            "t.initiate_user_id as initiateUserId, \n" +
            "t.initiate_user_name as initiateUserName,\n" +
            "t.join_user_id as joinUserId, \n" +
            "t.join_user_name as joinUserName,\n" +
            "t.chat_file as chatFile, \n" +
            "date_format(t.create_time , \"%Y-%m-%d %H:%k:%s\") as createTime \n" +
            "from t_expert_consultation t \n" +
            "where t.`status` = 1 \n" +
            "and t.join_user_id like concat(concat(\"%\",#{jsonUserId}) , \"%\") \n" +
            "order by t.create_time desc;")
    List<ExpertConsultation> selectConsultationListByJsonUserId(@Param("jsonUserId") String jsonUserId);

    @Select("select \n" +
            "t.id , t.title, \n" +
            "t.initiate_user_id as initiateUserId, \n" +
            "t.initiate_user_name as initiateUserName,\n" +
            "t.join_user_id as joinUserId, \n" +
            "t.join_user_name as joinUserName,\n" +
            "t.chat_file as chatFile, \n" +
            "date_format(t.create_time , \"%Y-%m-%d %H:%i:%s\") as createTime \n" +
            "from t_expert_consultation t \n" +
            "where t.`status` = 1 \n" +
            "and t.join_user_name like concat(concat(\"%\",#{joinUserName}) , \"%\") \n" +
            "and t.id = #{id}  \n" +
            "order by t.create_time desc")
    ExpertConsultation userExistsConsultation(@Param("id") int id, @Param("joinUserName")String joinUserName);


    @Select("select t.chat_file as chatFile  from t_expert_consultation t where t.id = #{id} and t.`status` = 1")
   String historyChat(@Param("id") int id);

    @Select("select \n" +
            "t.id , t.title, \n" +
            "t.initiate_user_id as initiateUserId, \n" +
            "t.initiate_user_name as initiateUserName,\n" +
            "t.join_user_id as joinUserId, \n" +
            "t.join_user_name as joinUserName,\n" +
            "t.chat_file as chatFile, \n" +
            "date_format(t.create_time , \"%Y-%m-%d %H:%i:%s\") as createTime \n" +
            "from t_expert_consultation t where t.id = #{id} and t.`status` = 1")
    ExpertConsultation selectByPrimaryKey(@Param("id") int id);


    @Insert("insert into t_expert_consultation_file(cid , upload_user , filename ,file_url) values(#{cid},#{uploadUserName},#{fileName},#{fileUrl})")
    int insertConsultationFile(@Param("cid") Integer cid , @Param("uploadUserName")String uploadUserName, @Param("fileName") String fileName , @Param("fileUrl") String fileUrl);



    @Select("select \n" +
            "t.id , \n" +
            "t.cid ,\n" +
            "t.upload_user as uploadUserName , \n" +
            "t.filename as fileName , \n" +
            "t.file_url as fileUrl ,\n" +
            "date_format(t.create_time , \"%Y-%m-%d %H:%k:%s\") as createTime \n" +
            "from t_expert_consultation_file t \n" +
            "where t.cid = #{consultationId}\n" +
            "and t.`status` = 1\n" +
            "order by t.create_time desc")
    List<ExpertConsultationFile> selectFileListByConsultationId(@Param("consultationId") Integer consultationId);

}